package com.smartao.base.position;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.ForeignKey;

import com.smartao.base.department.Department;
import com.smartao.base.employee.Employee;
import com.smartao.base.selectitem.Selectitem;
import com.smartao.core.orm.entity.BaseEntity;

/**
 * 岗位实体类
 * @author sunxing
 */
@Entity
@Table(name="T_BASE_POSITION")
public class Position extends BaseEntity<Position>{
	
	private static final long serialVersionUID = -6790214967381737155L;

	//编号
    private String code;
    
    private String name;
    
    //所属部门
    @ManyToOne
    @ForeignKey(name = "none")
	@JoinColumn(name = "department")
    private Department department;
    
    //岗位级别
    @ManyToOne
    @ForeignKey(name = "none")
	@JoinColumn(name = "rank")
    private Selectitem rank;
    //定编
    private Integer maxSize;
    //在编 只算主岗，兼岗不算；
    private Integer usedSize;
    
    private Integer dsporder;
    
    private String description;
    
	@ManyToMany(targetEntity = Employee.class, fetch = FetchType.LAZY)
	@JoinTable(name = "T_BASE_EMPLOYEE_POSITION", joinColumns = { @JoinColumn(name = "POSITION_ID", nullable = false) }, inverseJoinColumns = { @JoinColumn(name = "EMPLOYEE_ID", nullable = false) })
	@ForeignKey(name = "none", inverseName = "none")
    private List<Employee> employees;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
    
	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public Integer getMaxSize() {
		return maxSize;
	}

	public void setMaxSize(Integer maxSize) {
		this.maxSize = maxSize;
	}

	public Integer getUsedSize() {
		return usedSize;
	}

	public void setUsedSize(Integer usedSize) {
		this.usedSize = usedSize;
	}

	public Integer getDsporder() {
		return dsporder;
	}

	public void setDsporder(Integer dsporder) {
		this.dsporder = dsporder;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<Employee> getEmployees() {
		return employees;
	}

	public void setEmployees(List<Employee> employees) {
		this.employees = employees;
	}

	public Selectitem getRank() {
		return rank;
	}

	public void setRank(Selectitem rank) {
		this.rank = rank;
	}
	
	public void addUser(Employee employee){
		if(this.employees==null){
			this.employees = new ArrayList<Employee>();
		}
		if(!this.employees.contains(employee)){
			this.employees.add(employee);
		}
	}
}
